package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("/api/v1")
@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/addUser")
    @ResponseBody
    public Boolean addUser(@RequestBody User user) {

        return userService.addUser(user);
    }

    @RequestMapping("/deleteUser")
    @ResponseBody
    public Boolean deleteUser(Integer id) {

        return userService.deleteUser(id);
    }

    @RequestMapping("/updateUser")
    @ResponseBody
    public Boolean updateUser(@RequestBody User user) {

        return userService.addUser(user);
    }

    @RequestMapping("/selectUser/{id}")
    @ResponseBody
    public User selectUser(@PathVariable("id") Integer id) {
        User user = userService.selectUser(id);
        return user;
    }

    @RequestMapping("/selectUserByName/{name}")
    @ResponseBody
    public User selectUser(@PathVariable("name") String name) {
        System.out.println(name);
        return userService.findByUserName(name);
    }

    @RequestMapping("/selectUserBySex/{sex}")
    @ResponseBody
    public User selectUserBySex(@PathVariable("sex") String sex) {
        System.out.println(sex);
        return userService.findUserBySex(sex);
    }

    @RequestMapping("/index")
    public String index(Integer id, Model model) {
        model.addAttribute("user", userService.selectUser(id));
        return "index";

    }
}
